package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器广播当前场景角色
 */
public class WorldMap_InitRoleProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 13007; }

    public Integer RoleCount; //角色数量
    public ArrayList<RoleItem> ItemList; //角色列表

    /**
     * 角色列表
     */
    public static class RoleItem
    {
        public Integer RoleId; //角色编号
        public String RoleNickName; //角色昵称
        public Integer RoleLevel; //角色等级
        public Integer RoleMaxHP; //最大血量
        public Integer RoleCurrHP; //当前血量
        public Integer RoleMaxMP; //最大魔法
        public Integer RoleCurrMP; //当前魔法
        public Integer RoleJobId; //角色职业编号
        public Float RolePosX; //角色坐标X
        public Float RolePosY; //角色坐标Y
        public Float RolePosZ; //角色坐标Z
        public Float RoleYAngle; //角色Y轴旋转
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(RoleCount);
            for (int i = 0; i < RoleCount; i++) {
                dataStream.WriteInt(ItemList.get(i).RoleId);
                dataStream.WriteUTF8String(ItemList.get(i).RoleNickName);
                dataStream.WriteInt(ItemList.get(i).RoleLevel);
                dataStream.WriteInt(ItemList.get(i).RoleMaxHP);
                dataStream.WriteInt(ItemList.get(i).RoleCurrHP);
                dataStream.WriteInt(ItemList.get(i).RoleMaxMP);
                dataStream.WriteInt(ItemList.get(i).RoleCurrMP);
                dataStream.WriteInt(ItemList.get(i).RoleJobId);
                dataStream.WriteFloat(ItemList.get(i).RolePosX);
                dataStream.WriteFloat(ItemList.get(i).RolePosY);
                dataStream.WriteFloat(ItemList.get(i).RolePosZ);
                dataStream.WriteFloat(ItemList.get(i).RoleYAngle);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static WorldMap_InitRoleProto GetProto(byte[] buffer) {
        WorldMap_InitRoleProto proto = new WorldMap_InitRoleProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.RoleCount = dataStream.ReadInt();
            proto.ItemList = new ArrayList<RoleItem>();
            for (int i = 0; i < proto.RoleCount; i++) {
                RoleItem _Item = new RoleItem();
                _Item.RoleId = dataStream.ReadInt();
                _Item.RoleNickName = dataStream.ReadUTF8String();
                _Item.RoleLevel = dataStream.ReadInt();
                _Item.RoleMaxHP = dataStream.ReadInt();
                _Item.RoleCurrHP = dataStream.ReadInt();
                _Item.RoleMaxMP = dataStream.ReadInt();
                _Item.RoleCurrMP = dataStream.ReadInt();
                _Item.RoleJobId = dataStream.ReadInt();
                _Item.RolePosX = dataStream.ReadFloat();
                _Item.RolePosY = dataStream.ReadFloat();
                _Item.RolePosZ = dataStream.ReadFloat();
                _Item.RoleYAngle = dataStream.ReadFloat();
                proto.ItemList.add(_Item);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}